package com.shsoft.controller;

import com.shsoft.MonitorApplication;
import com.shsoft.component.BarComponent;
import com.shsoft.component.BaseComponent;
import com.shsoft.component.DataComponent;
import com.shsoft.config.PCStatusConstant;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import org.springframework.util.StringUtils;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class ComponentChooseController implements Initializable {

    @FXML
    private ComboBox<String> componentType;

    @FXML
    private ComboBox<String> componentName;

    @FXML
    private Label componentNameLabel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componentType.getItems().addAll(PCStatusConstant.COMPONENT_TEXT, PCStatusConstant.COMPONENT_IMAGE, PCStatusConstant.COMPONENT_SHAPE, PCStatusConstant.COMPONENT_BAR, PCStatusConstant.COMPONENT_CPU,
                 PCStatusConstant.COMPONENT_GPU, PCStatusConstant.COMPONENT_MEM, PCStatusConstant.COMPONENT_DATE, PCStatusConstant.COMPONENT_WEATHER);
        componentType.setValue(PCStatusConstant.COMPONENT_TEXT);
        componentType.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            ArrayList<BaseComponent> componentList = MonitorApplication.mainController.componentList;
            ObservableList<String> items;
            switch (newValue){
                case PCStatusConstant.COMPONENT_TEXT:
                case PCStatusConstant.COMPONENT_IMAGE:
                case PCStatusConstant.COMPONENT_SHAPE:
                    componentName.setVisible(false);
                    componentNameLabel.setVisible(false);
                    break;
                case PCStatusConstant.COMPONENT_BAR:
                    items = FXCollections.observableArrayList(PCStatusConstant.COMPONENT_BAR_CPU_TEMP,PCStatusConstant.COMPONENT_BAR_CPU_USAGE,PCStatusConstant.COMPONENT_BAR_GPU_TEMP,PCStatusConstant.COMPONENT_BAR_GPU_USAGE,
                            PCStatusConstant.COMPONENT_BAR_MEM_USAGE,PCStatusConstant.COMPONENT_BAR_WEATHER_TEMP,PCStatusConstant.COMPONENT_BAR_WEATHER_HUM);
                    doComponentNames(componentList, items, BaseComponent.ComponentType.BAR);
                    break;
                case PCStatusConstant.COMPONENT_CPU:
                    items = FXCollections.observableArrayList(PCStatusConstant.COMPONENT_CPU_NAME,PCStatusConstant.COMPONENT_CPU_TEMP,
                            PCStatusConstant.COMPONENT_CPU_USAGE,PCStatusConstant.COMPONENT_CPU_CLOCK,PCStatusConstant.COMPONENT_CPU_POWER);
                    doComponentNames(componentList, items, BaseComponent.ComponentType.DATA);
                    break;
                case PCStatusConstant.COMPONENT_GPU:
                    items = FXCollections.observableArrayList(PCStatusConstant.COMPONENT_GPU_NAME,PCStatusConstant.COMPONENT_GPU_TEMP,
                            PCStatusConstant.COMPONENT_GPU_USAGE,PCStatusConstant.COMPONENT_GPU_CLOCK,PCStatusConstant.COMPONENT_GPU_POWER);
                    doComponentNames(componentList, items, BaseComponent.ComponentType.DATA);
                    break;
                case PCStatusConstant.COMPONENT_MEM:
                    items = FXCollections.observableArrayList(PCStatusConstant.COMPONENT_MEM_NAME,PCStatusConstant.COMPONENT_MEM_CLOCK,
                            PCStatusConstant.COMPONENT_MEM_USAGE,PCStatusConstant.COMPONENT_NET_UPLOAD_SPEED,PCStatusConstant.COMPONENT_NET_DOWN_SPEED);
                    doComponentNames(componentList, items, BaseComponent.ComponentType.DATA);
                    break;
                case PCStatusConstant.COMPONENT_DATE:
                    items = FXCollections.observableArrayList(PCStatusConstant.COMPONENT_DATE_YEAR,PCStatusConstant.COMPONENT_DATE_MONTH,PCStatusConstant.COMPONENT_DATE_DAY,PCStatusConstant.COMPONENT_DATE_WEEK,
                            PCStatusConstant.COMPONENT_DATE_HOUR,PCStatusConstant.COMPONENT_DATE_MIN,PCStatusConstant.COMPONENT_DATE_SEC);
                    doComponentNames(componentList, items, BaseComponent.ComponentType.DATA);
                    break;
                case PCStatusConstant.COMPONENT_WEATHER:
                    items = FXCollections.observableArrayList(PCStatusConstant.COMPONENT_WEATHER_TEMP,PCStatusConstant.COMPONENT_WEATHER_HUM,PCStatusConstant.COMPONENT_WEATHER_CITY,
                            PCStatusConstant.COMPONENT_WEATHER_AIR,PCStatusConstant.COMPONENT_WEATHER_STRING);
                    doComponentNames(componentList, items, BaseComponent.ComponentType.DATA);
                    break;
                default:
                    break;
            }
        });
    }

    @FXML
    void insertComponent(ActionEvent event) {
        String type = componentType.getValue();
        if(type.equals(PCStatusConstant.COMPONENT_WEATHER) && StringUtils.isEmpty(MonitorApplication.settingConfig.getCityID())){
            MonitorApplication.mainController.showWarning("请先在首选项中绑定和风天气，再添加天气相关的组件");
            return;
        }
        String name = componentName.getValue();
        MonitorApplication.mainController.generateComponent(type, name);
        MonitorApplication.componentStage.hide();
    }

    private void doComponentNames(ArrayList<BaseComponent> componentList, ObservableList<String> items, BaseComponent.ComponentType type){
        componentName.setVisible(true);
        componentNameLabel.setVisible(true);
        if(type == BaseComponent.ComponentType.DATA){
            componentNameLabel.setText("组件");
        }else if(type == BaseComponent.ComponentType.BAR){
            componentNameLabel.setText("关联数据");
        }
        if(null != componentList && componentList.size() > 0){
            for (BaseComponent baseComponent : componentList) {
                if(baseComponent.getType() == type && type == BaseComponent.ComponentType.DATA){
                    DataComponent component = (DataComponent) baseComponent;
                    items.remove(component.getName());
                }else if(baseComponent.getType() == type && type == BaseComponent.ComponentType.BAR){
                    BarComponent component = (BarComponent) baseComponent;
                    items.remove(component.getName());
                }
            }
        }
        componentName.setItems(items);
        if(items.size() > 0){
            componentName.setValue(items.get(0));
        }
    }
}
